package com.ruoyi.project.declaration.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.declaration.domain.FileList;
import com.ruoyi.project.declaration.domain.Files;
import com.ruoyi.project.declaration.service.IFilesService;
import com.ruoyi.project.declaration.service.IProjectRecordService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.declaration.domain.Project;
import com.ruoyi.project.declaration.service.IProjectService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 工程认证申报数据Controller
 * 
 * @author Daniel
 * @date 2020-03-24
 */
@RestController
@RequestMapping("/declaration/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    @Autowired
    private IFilesService filesService;



    /**
     * 查询工程认证申报数据列表
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出工程认证申报数据列表
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:export')")
    @Log(title = "工程认证申报数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 获取工程认证申报数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:query')")
    @GetMapping(value = "/{pId}")
    public AjaxResult getInfo(@PathVariable("pId") Long pId)
    {
        return AjaxResult.success(projectService.selectProjectById(pId));
    }

    /**
     * 新增工程认证申报数据
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:add')")
    @Log(title = "工程认证申报数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        project.setpApplicantId(SecurityUtils.getLoginUser().getUser().getUserId());
        project.setpProgressId("1");
        final int i = projectService.insertProject(project);
        //插入文件表数据
        for(FileList fl:project.getFileList()){
            Files file=new Files();
            file.setfName(fl.getName());
            file.setfPath(fl.getUrl());
            file.setfDowloads((long) 0);
            file.setfType((long) 1);
            file.setfSourceId(project.getpId());
            file.setfSourceName(project.getpName());
            filesService.insertFiles(file);
        }
        return toAjax(i);
    }

    /**
     * 修改工程认证申报数据
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:edit')")
    @Log(title = "工程认证申报数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        project.setpApplicantId(SecurityUtils.getLoginUser().getUser().getUserId());
        final int i = projectService.updateProject(project);
        Files files=new Files();
        files.setfSourceId(project.getpId());
        List<Files> filesList = filesService.selectFilesList(files);
        for(Files fl:filesList){
            filesService.deleteFilesById(fl.getfId());
        }

        for(FileList fl:project.getFileList()){
            Files f=new Files();
            f.setfSourceId(project.getpId());
            f.setfSourceName(project.getpName());
            f.setfDowloads((long) 0);
            f.setfPath(fl.getUrl());
            f.setfName(fl.getName());
            f.setfType((long) 1);
            f.setfDowloads(fl.getfDowloads()==null?0:fl.getfDowloads());
            filesService.insertFiles(f);
        }
        return toAjax(i);
    }

    /**
     * 删除工程认证申报数据
     */
    @PreAuthorize("@ss.hasPermi('declaration:project:remove')")
    @Log(title = "工程认证申报数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pIds}")
    public AjaxResult remove(@PathVariable Long[] pIds)
    {
        return toAjax(projectService.deleteProjectByIds(pIds));
    }
}
